/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.confiteriofuentes.modelo;


import com.confiteriafuentes.modelo.distribucion.Conductor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Administrador
 */
public class EmpleadoDAO {



    private Model model;
    private PreparedStatement psInsert;


    public EmpleadoDAO() throws SQLException {
        model = Model.getInstance();
    }

    public void insert(Empleado t) throws SQLException {


        if (psInsert == null) {
            String sqlInsert = "INSERT INTO empleado (nombre, salario, tipo) VALUES (?, ?, ?)";
            psInsert = (PreparedStatement) model.getCon().prepareStatement(sqlInsert);
            
            
       psInsert.setString(1, t.getNombre());
       psInsert.setInt(2, t.getSalario());
       psInsert.setString(3,t.getTipoempleado());


        psInsert.executeUpdate();
        }

       
    }

        public Conductor  getConductor(int cod) throws SQLException {
   ResultSet rs = null;
Conductor c= new Conductor();
        if (psInsert == null) {
            String sqlInsert = "SELECT * FROM empleado WHERE codigo_empleado=? and tipo='Conductor'";
            psInsert = (PreparedStatement) model.getCon().prepareStatement(sqlInsert);


       psInsert.setInt(1,cod);
      rs = psInsert.executeQuery();

             while(rs.next()) {
             String nom= rs.getString("nombre");
             c.setNombre(nom);

         }



        }
   return c;

    }





     public  int NumRegistros()throws SQLException{
         

        int numfil=0;
            if (model.getCon() == null ) {
          throw new SQLException(" no hay conexion ");
         }
         ResultSet rs = null;
          PreparedStatement pst = null;
           try {
            pst = model.getCon().prepareStatement("select count(*) AS  numfil from empleado");
            rs = pst.executeQuery();

             while(rs.next()) {
             numfil = rs.getInt("numfil");
         }
            }finally {
        if (pst != null) {
             pst.close();
        }

   }
         return numfil;
    }
   



       public  int IncrementarCod()throws SQLException{
        int cod=0;

            if (model.getCon() == null ) {
          throw new SQLException(" no hay conexion ");
         }
         ResultSet rs = null;
          PreparedStatement pst = null;
           try {
            pst = model.getCon().prepareStatement("select max(codigo_empleado)+1 AS codInt from empleado");
            rs = pst.executeQuery();

             while(rs.next()) {
             cod= rs.getInt("codInt");
         }
            }finally {
        if (pst != null) {
             pst.close();
        }

   }
         return cod;
    }



        public List<Empleado> getAll() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Empleado find(Integer id) throws SQLException {

        

                  Connection conex=(Connection) model.getCon();

                if (!model.isConnect())
			throw new SQLException("no hay conexcion");
                Empleado e= new Empleado();
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			
                    pstmt=(PreparedStatement) conex.prepareStatement("SELECT * FROM empleado WHERE codigo_empleado=?");
                    	pstmt.setInt(1,id.intValue());
			rs=pstmt.executeQuery();
			if(rs.next()){

                        }else{

                        }
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return e;
    }


    public void delete(Empleado t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(Empleado t) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}


